//
//  DTKNetManger.m
//  DTBaseTool_Example
//
//  Created by 葛悦彣 on 2023/2/20.
//  Copyright © 2023 GGG. All rights reserved.
//

#import "DTKNetManger.h"

@implementation DTKNetManger

+ (id)GET:(NSString *)path apiKey:(NSString *)apiKey parameters:(id)parameters progress:(void(^)(NSProgress *downloadProgress))downloadProgress completionHandler:(void(^)(id responseObj, NSError *error))completionHandler;{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html", @"text/plain", @"text/json", @"text/javascript", @"application/json"]];
    [manager.requestSerializer setValue:apiKey forHTTPHeaderField:@"apikey"];
    manager.requestSerializer.timeoutInterval = 30;
    
    return [manager GET:path parameters:parameters headers:@{} progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completionHandler(nil, error);
        NSLog(@"error %@", error);
    }];
    
}

+ (id)GET:(NSString *)path parameters:(id)parameters progress:(void(^)(NSProgress *downloadProgress))downloadProgress completionHandler:(void(^)(id responseObj, NSError *error))completionHandler;{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html", @"text/plain", @"text/json", @"text/javascript", @"application/json"]];
    manager.requestSerializer.timeoutInterval = 30;
    
    return [manager GET:path parameters:parameters headers:@{} progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completionHandler(nil, error);
        NSLog(@"error %@", error);
    }];
    
    
}

+ (id)POST:(NSString *)path parameters:(id)parameters progress:(void (^)(NSProgress *))downloadProgress completionHandler:(void (^)(id, NSError *))completionHandler {
    NSLog(@"->接口开始：------");
    CFAbsoluteTime startTime =CFAbsoluteTimeGetCurrent();
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html", @"text/plain", @"text/json", @"text/javascript", @"application/json"]];
    manager.requestSerializer.timeoutInterval = 30;
    NSString *token = [[NSUserDefaults standardUserDefaults] valueForKey:@"token"];
    NSString *testStr = [NSString stringWithFormat:@"%@%@",@"Bearer ",token];
    [manager.requestSerializer setValue:testStr forHTTPHeaderField:@"Authorization"];
    
    return [manager POST:path parameters:parameters headers:@{} progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        CFAbsoluteTime linkTime = (CFAbsoluteTimeGetCurrent() - startTime);

        NSLog(@"->%@,接口用时：%f ms",path,linkTime *1000.0);
        NSLog(@"path->%@",path);
        NSLog(@"parameters->%@",parameters);
        NSLog(@"responseObject->%@",responseObject);
        completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        CFAbsoluteTime linkTime = (CFAbsoluteTimeGetCurrent() - startTime);
        NSLog(@"->%@,Linked in %f ms",path,linkTime *1000.0);
        completionHandler(nil, error);
    }];
    
    
}

//post 请求头
+ (id)POST:(NSString *)path parameters:(id)parameters headerField:(NSString *)token progress:(void (^)(NSProgress *))downloadProgress completionHandler:(void (^)(id, NSError *))completionHandler{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html", @"text/plain", @"text/json", @"text/javascript", @"application/json",@"text/xml"]];
    manager.requestSerializer.timeoutInterval = 30;
     NSString *testStr = [NSString stringWithFormat:@"%@%@",@"Bearer ",token];
    [manager.requestSerializer setValue:testStr forHTTPHeaderField:@"Authorization"];
    
    return [manager POST:path parameters:parameters headers:@{} progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        completionHandler(nil, error);
    }];
    
}



@end
